package com.ouyy.volunteerservice.volunteer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ouyy.volunteerservice.volunteer.domain.po.VolunteerActivities;
import com.ouyy.volunteerservice.volunteer.service.InfoNoticeService;
import com.ouyy.volunteerservice.volunteer.service.VolunteerActivateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/activities")
public class VolunteerActivitiesController {

    @Autowired
    private VolunteerActivateService activateService;

    // ✅ 获取所有活动
    @GetMapping
    public List<VolunteerActivities> getAllActivities() {
        return activateService.list();
    }

    // ✅ 根据 ID 查询活动
    @GetMapping("/{id}")
    public VolunteerActivities getActivityById(@PathVariable("id") Integer activateId) {
        return activateService.getById(activateId);
    }

    // ✅ 新增活动
    @PostMapping
    public boolean createActivity(@RequestBody VolunteerActivities activity) {

        return activateService.save(activity);
    }

    // ✅ 更新活动，包括报名人数
    @PutMapping("/{id}")
    public boolean updateActivity(@PathVariable Integer id, @RequestBody VolunteerActivities activity) {
        // 设置活动ID
        activity.setActivityId(id);
        // 获取当前活动的报名人数，进行相应的更新
        VolunteerActivities existingActivity = activateService.getById(id); // 假设通过id获取活动
        if (existingActivity != null) {
            // 增加报名人数（如果你只是增加报名人数，而不是直接设置）
            existingActivity.setRegisteredNumber(existingActivity.getRegisteredNumber() + 1);
        } else {
            // 活动不存在，返回失败
            return false;
        }

        // 执行更新
        return activateService.updateById(existingActivity);
    }

    // ✅ 删除活动
    @DeleteMapping("/{id}")
    public boolean deleteActivity(@PathVariable Integer id) {
        return activateService.removeById(id);
    }

    // ✅ 根据输入条件查询（动态查询）
    @GetMapping("/search")
    public List<VolunteerActivities> searchActivities(
            @RequestParam(required = false) String activityName,
            @RequestParam(required = false) String activityStatus,
            @RequestParam(required = false) String serviceCategory,
            @RequestParam(required = false) String initiator,
            @RequestParam(required = false) String address,
            @RequestParam(required = false) String organizationId

    ) {
        QueryWrapper<VolunteerActivities> queryWrapper = new QueryWrapper<>();

        if (activityName != null && !activityName.isEmpty()) {
            queryWrapper.like("activity_name", activityName); // 模糊查询
        }
        if (activityStatus != null && !activityStatus.isEmpty()) {
            queryWrapper.eq("activity_status", activityStatus); // 精确查询
        }
        if (serviceCategory != null && !serviceCategory.isEmpty()) {
            queryWrapper.eq("service_category", serviceCategory); // 精确查询
        }
        if (initiator != null && !initiator.isEmpty()) {
            queryWrapper.eq("initiator", initiator); // 精确查询
        }if(address != null && !address.isEmpty()){
            queryWrapper.eq("address",address);
        }if (organizationId != null && !organizationId.isEmpty()){
            queryWrapper.eq("organization_id",organizationId);
        }

        return activateService.list(queryWrapper);
    }

    @PostMapping("/update")
    public boolean updateActivity(@RequestBody VolunteerActivities activity){
        // 调用Service层方法更新活动
        boolean updated = activateService.updateActivity(activity);
        // 返回更新结果
        return updated;
    }


    // 获取按活动类别统计的活动数量
    @GetMapping("/countByCategory")
    public List<Map<String, Object>> countActivitiesByCategory() {
        return activateService.countActivitiesByCategory();
    }

    @GetMapping("/countByMounth")
    public List<Map<String, Object>> countMounth() {
        return activateService.countActivitiesByMonth();
    }
}
